﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.DataEntity;
using ERP.Manager.BusinessServices;
using ERP.Manager.Controllers.CustomAttributes;
using System.Web.Mvc;

namespace ERP.Manager.Controllers
{
    [CheckLoginAttribute]
    public class WorkBlogController : Controller
    {

        public readonly WorkBloginfoService workBlogService;
        private readonly UserinfoService userservice;
        private readonly ContextService contextservcie;
        public WorkBlogController() { }
        public WorkBlogController(WorkBloginfoService workblogservice, UserinfoService userservces, ContextService contextservice)
        {
            this.workBlogService = workblogservice;
            this.userservice = userservces;
            this.contextservcie = contextservice;
        }

        public ActionResult AddWorkBlog(string workblogid)
        {
            if (workblogid == "0")
            {
                return View();
            }
            else
            {
                ViewData["WorkBlog"] = workBlogService.getWorkBlogById(int.Parse(workblogid));
                return View();
            }
        }
        [CheckPersonnel]
        public ActionResult WorkBlogList(int? page)
        {
            int pagecount = page ?? 1;
            List<WorkBloginfo> list = workBlogService.getWorkBlogByUserid(contextservcie.UserId, pagecount);
            int count = workBlogService.getallworkblogcountbyuserid(contextservcie.UserId);
            ViewData["count"] = count;
            ViewData["workblogs"] = list;
            return View();

        }

        [CheckPersonnel]
        public ActionResult WorkList(string userId, int? page)
        {
            int pagecount = page ?? 1;
            List<WorkBloginfo> list = workBlogService.getWorkBlogByUserid(userId, pagecount);
            int count = workBlogService.getallworkblogcountbyuserid(userId);
            ViewData["count"] = count;
            ViewData["workblogs"] = list;
            return View("WorkBlogList", ViewData["workblogs"]);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public string DeleteWorkBlog(int workblogId)
        {
            try
            {
                int rowcount = workBlogService.DeleteWorkBlog(workblogId);
                if (rowcount > 0)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        //添加以及修改个人工作信息
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public string AjaxAddWorkBlog(string blogid, string worktitle, string contenText, string notetext)
        {
            try
            {
                if (blogid == "")
                {
                    WorkBloginfo workblog = new WorkBloginfo();
                    workblog.workblogTitle = worktitle;
                    workblog.workContent = contenText;
                    workblog.userid = contextservcie.UserId;
                    workblog.note = notetext;
                    workblog.starTime = DateTime.Now.AddDays(0);
                    int rowcount = workBlogService.addWorkBlog(workblog);
                    if (rowcount > 0)
                    {
                        System.Threading.Thread.Sleep(1000);
                        return "您添加工作信息成功！！";
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1000);
                        return "您添加工作信息失败！！";
                    }
                }
                else
                {
                    WorkBloginfo workblog = new WorkBloginfo();
                    workblog.workblogId = int.Parse(blogid.ToString().Trim());
                    workblog.workblogTitle = worktitle;
                    workblog.workContent = contenText;
                    workblog.note = notetext;
                    int rowcount = workBlogService.UpdateWorkBlog(workblog);
                    if (rowcount > 0)
                    {
                        System.Threading.Thread.Sleep(1000);
                        return "您修改工作信息成功！！";
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1000);
                        return "您修改工作信息失败！！";
                    }
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #region 个人日志详细页面
        public ActionResult WorkBlogListDetail(string blogId)
        {
            int WorkblogId = int.Parse(blogId.ToString());
            WorkBloginfo workblog = workBlogService.getWorkBlogById(WorkblogId);
            UserInfo userinfo = userservice.DetailUserinfo(workblog.userid.Trim());
            ViewData["userinfo"] = userinfo;
            ViewData["workblog"] = workblog;
            return View();
        }
        #endregion
    }
}
